

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>ccqw.fileBrowser &mdash; CCQW 0.1.1 documentation</title>
    
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '0.1.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/sidebar.js"></script>
    <link rel="top" title="CCQW 0.1.1 documentation" href="../../index.html" />
    <link rel="up" title="Module code" href="../index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">CCQW 0.1.1 documentation</a> &raquo;</li>
          <li><a href="../index.html" accesskey="U">Module code</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for ccqw.fileBrowser</h1><div class="highlight"><pre>
<span class="c"># -*- coding: utf-8 -*-</span>
<span class="sd">&quot;&quot;&quot; A class to browse a file. If it&#39;s **NOT** a text file do nothing</span>

<span class="sd">    :Author:</span>
<span class="sd">      - 20091202-20100321 Nicola Creati</span>
<span class="sd">      - 20111202-20120214 Roberto Vidmar</span>

<span class="sd">    :Revision:  $Revision: 52 $</span>
<span class="sd">                $Date: 2012-11-12 10:07:15 +0100 (Mon, 12 Nov 2012) $</span>

<span class="sd">    :Copyright: 2011-2012</span>
<span class="sd">                Nicola Creati &lt;ncreati@inogs.it&gt;</span>
<span class="sd">                Roberto Vidmar &lt;rvidmar@inogs.it&gt;</span>

<span class="sd">    :License: MIT/X11 License (see :download:`license.txt</span>
<span class="sd">                               &lt;../../license.txt&gt;`)</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">from</span> <span class="nn">string</span> <span class="kn">import</span> <span class="n">maketrans</span>
<span class="kn">from</span> <span class="nn">qtCompat</span> <span class="kn">import</span> <span class="n">QtCore</span><span class="p">,</span> <span class="n">QtGui</span>

<div class="viewcode-block" id="isText"><a class="viewcode-back" href="../../ccqw.html#ccqw.fileBrowser.isText">[docs]</a><span class="k">def</span> <span class="nf">isText</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">threshold</span><span class="o">=</span><span class="mf">0.30</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot; Return True if s is a Text string.</span>

<span class="sd">      If more than 30% non-text characters, then this is considered a binary</span>
<span class="sd">      string</span>

<span class="sd">    :param s: string to test</span>
<span class="sd">    :type s: string</span>
<span class="sd">    :param threshold: threshold to make the decision</span>
<span class="sd">    :type threshold: float [0, 1], no check</span>
<span class="sd">    :returns: True if s Dacontains pure Text</span>
<span class="sd">    :rtype: bool</span>
<span class="sd">    :raises:</span>
<span class="sd">  &quot;&quot;&quot;</span>

  <span class="n">text_characters</span> <span class="o">=</span> <span class="s">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">chr</span><span class="p">,</span> <span class="nb">range</span><span class="p">(</span><span class="mi">32</span><span class="p">,</span> <span class="mi">127</span><span class="p">))</span> <span class="o">+</span> <span class="nb">list</span><span class="p">(</span><span class="s">&quot;</span><span class="se">\n\r\t\b</span><span class="s">&quot;</span><span class="p">))</span>
  <span class="n">null_trans</span> <span class="o">=</span> <span class="n">maketrans</span><span class="p">(</span><span class="s">&quot;&quot;</span><span class="p">,</span> <span class="s">&quot;&quot;</span><span class="p">)</span>

  <span class="k">if</span> <span class="s">&quot;</span><span class="se">\0</span><span class="s">&quot;</span> <span class="ow">in</span> <span class="n">s</span><span class="p">:</span>
    <span class="k">return</span> <span class="bp">False</span>

  <span class="k">if</span> <span class="ow">not</span> <span class="n">s</span><span class="p">:</span>  <span class="c"># Empty files are considered text</span>
    <span class="k">return</span> <span class="bp">True</span>

  <span class="c"># Get the non-text characters (maps a character to itself then</span>
  <span class="c"># use the &#39;remove&#39; option to get rid of the text characters.)</span>
  <span class="n">t</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">translate</span><span class="p">(</span><span class="n">null_trans</span><span class="p">,</span> <span class="n">text_characters</span><span class="p">)</span>

  <span class="c"># If more than 30% non-text characters, then</span>
  <span class="c"># this is considered a binary string</span>
  <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="n">t</span><span class="p">)</span><span class="o">/</span><span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="n">threshold</span>
</div>
<div class="viewcode-block" id="isTextFile"><a class="viewcode-back" href="../../ccqw.html#ccqw.fileBrowser.isTextFile">[docs]</a><span class="k">def</span> <span class="nf">isTextFile</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">blocksize</span><span class="o">=</span><span class="mi">512</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot; Return True if filename is a Text file.</span>

<span class="sd">    :param filename: pathname</span>
<span class="sd">    :type filename: string</span>
<span class="sd">    :param blocksize: number of bytes to read to guess the type</span>
<span class="sd">    :type blocksize: int</span>
<span class="sd">    :returns: True if filename is a Text file</span>
<span class="sd">    :rtype: bool</span>
<span class="sd">    :raises:</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="k">return</span> <span class="n">isText</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">blocksize</span><span class="p">))</span>
</div>
<div class="viewcode-block" id="FileBrowser"><a class="viewcode-back" href="../../ccqw.html#ccqw.fileBrowser.FileBrowser">[docs]</a><span class="k">class</span> <span class="nc">FileBrowser</span><span class="p">(</span><span class="n">QtGui</span><span class="o">.</span><span class="n">QMainWindow</span><span class="p">):</span>
<div class="viewcode-block" id="FileBrowser.__init__"><a class="viewcode-back" href="../../ccqw.html#ccqw.fileBrowser.FileBrowser.__init__">[docs]</a>  <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parent</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Create a new instance of the FileBrowser.</span>

<span class="sd">    :param parent: parent widget</span>
<span class="sd">    :type parent: QtGui widget</span>
<span class="sd">    :param path: pathname of the file to browse</span>
<span class="sd">    :type path: string, unicode</span>
<span class="sd">    :raises:</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="nb">super</span><span class="p">(</span><span class="n">FileBrowser</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="n">parent</span><span class="p">)</span>

    <span class="bp">self</span><span class="o">.</span><span class="n">editor</span> <span class="o">=</span> <span class="n">QtGui</span><span class="o">.</span><span class="n">QTextEdit</span><span class="p">()</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">editor</span><span class="o">.</span><span class="n">setReadOnly</span><span class="p">(</span><span class="bp">True</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">openFile</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">highlighter</span> <span class="o">=</span> <span class="n">Highlighter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">editor</span><span class="o">.</span><span class="n">document</span><span class="p">())</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">setCentralWidget</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">editor</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="FileBrowser.openFile"><a class="viewcode-back" href="../../ccqw.html#ccqw.fileBrowser.FileBrowser.openFile">[docs]</a>  <span class="k">def</span> <span class="nf">openFile</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">pn</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Load pn file content into widget.</span>

<span class="sd">    :param pn: pathname</span>
<span class="sd">    :type pn:  string or unicode</span>
<span class="sd">    :raises:</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">pn</span><span class="p">:</span>
      <span class="n">inFile</span> <span class="o">=</span> <span class="n">QtCore</span><span class="o">.</span><span class="n">QFile</span><span class="p">(</span><span class="n">pn</span><span class="p">)</span>
      <span class="k">if</span> <span class="n">inFile</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">QtCore</span><span class="o">.</span><span class="n">QFile</span><span class="o">.</span><span class="n">ReadOnly</span> <span class="o">|</span> <span class="n">QtCore</span><span class="o">.</span><span class="n">QFile</span><span class="o">.</span><span class="n">Text</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">isTextFile</span><span class="p">(</span><span class="n">pn</span><span class="p">):</span>
          <span class="n">font</span> <span class="o">=</span> <span class="n">QtGui</span><span class="o">.</span><span class="n">QFont</span><span class="p">(</span><span class="s">&quot;Courier New&quot;</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="n">QtGui</span><span class="o">.</span><span class="n">QFont</span><span class="o">.</span><span class="n">DemiBold</span><span class="p">)</span>
          <span class="bp">self</span><span class="o">.</span><span class="n">editor</span><span class="o">.</span><span class="n">setCurrentFont</span><span class="p">(</span><span class="n">font</span><span class="p">)</span>
          <span class="bp">self</span><span class="o">.</span><span class="n">editor</span><span class="o">.</span><span class="n">setPlainText</span><span class="p">(</span><span class="nb">unicode</span><span class="p">(</span><span class="n">inFile</span><span class="o">.</span><span class="n">readAll</span><span class="p">()))</span>
</div></div>
<div class="viewcode-block" id="Highlighter"><a class="viewcode-back" href="../../ccqw.html#ccqw.fileBrowser.Highlighter">[docs]</a><span class="k">class</span> <span class="nc">Highlighter</span><span class="p">(</span><span class="n">QtGui</span><span class="o">.</span><span class="n">QSyntaxHighlighter</span><span class="p">):</span>
  <span class="n">KEYWORDS</span> <span class="o">=</span> <span class="p">(</span>
    <span class="s">&quot;description&quot;</span><span class="p">,</span>
    <span class="s">&quot;samples&quot;</span><span class="p">,</span>
    <span class="s">&quot;lines&quot;</span><span class="p">,</span>
    <span class="s">&quot;bands&quot;</span><span class="p">,</span>
    <span class="s">&quot;header offset&quot;</span><span class="p">,</span>
    <span class="s">&quot;file type&quot;</span><span class="p">,</span>
    <span class="s">&quot;data type&quot;</span><span class="p">,</span>
    <span class="s">&quot;interleave&quot;</span><span class="p">,</span>
    <span class="s">&quot;sensor type&quot;</span><span class="p">,</span>
    <span class="s">&quot;byte order&quot;</span><span class="p">,</span>
    <span class="s">&quot;map info&quot;</span><span class="p">,</span>
    <span class="s">&quot;default bands&quot;</span><span class="p">,</span>
    <span class="s">&quot;wavelength units&quot;</span><span class="p">,</span>
    <span class="s">&quot;band names&quot;</span><span class="p">,</span>
    <span class="s">&quot;fwhm&quot;</span><span class="p">,</span>
    <span class="s">&quot;vimg&quot;</span><span class="p">,</span>
    <span class="s">&quot;wavelength&quot;</span><span class="p">,</span>
    <span class="s">&quot;sensorid&quot;</span><span class="p">,</span>
    <span class="s">&quot;radcorr version&quot;</span><span class="p">,</span>
    <span class="s">&quot;acquisition date&quot;</span><span class="p">,</span>
    <span class="s">&quot;GPS Start Time&quot;</span><span class="p">,</span>
    <span class="s">&quot;GPS Stop Time&quot;</span><span class="p">,</span>
    <span class="s">&quot;GPS location&quot;</span><span class="p">,</span>
    <span class="s">&quot;x start&quot;</span><span class="p">,</span>
    <span class="s">&quot;y start&quot;</span><span class="p">,</span>
    <span class="s">&quot;fps&quot;</span><span class="p">,</span>
    <span class="s">&quot;binning&quot;</span><span class="p">,</span>
    <span class="s">&quot;vroi&quot;</span><span class="p">,</span>
    <span class="s">&quot;hroi&quot;</span><span class="p">,</span>
    <span class="s">&quot;tint&quot;</span><span class="p">,</span>
    <span class="s">&quot;himg&quot;</span><span class="p">,</span>
    <span class="s">&quot;fodis&quot;</span><span class="p">,</span>
    <span class="s">&quot;errors&quot;</span><span class="p">,</span>
    <span class="s">&quot;Wavelength&quot;</span><span class="p">)</span>

<div class="viewcode-block" id="Highlighter.__init__"><a class="viewcode-back" href="../../ccqw.html#ccqw.fileBrowser.Highlighter.__init__">[docs]</a>  <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parent</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Create a new instance of the Highlighter.</span>

<span class="sd">    :param parent: parent widget</span>
<span class="sd">    :type parent: QtGui widget</span>
<span class="sd">    :raises:</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="nb">super</span><span class="p">(</span><span class="n">Highlighter</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="n">parent</span><span class="p">)</span>

    <span class="n">keywordFormat</span> <span class="o">=</span> <span class="n">QtGui</span><span class="o">.</span><span class="n">QTextCharFormat</span><span class="p">()</span>
    <span class="n">keywordFormat</span><span class="o">.</span><span class="n">setForeground</span><span class="p">(</span><span class="n">QtCore</span><span class="o">.</span><span class="n">Qt</span><span class="o">.</span><span class="n">darkBlue</span><span class="p">)</span>
    <span class="n">keywordFormat</span><span class="o">.</span><span class="n">setFontWeight</span><span class="p">(</span><span class="n">QtGui</span><span class="o">.</span><span class="n">QFont</span><span class="o">.</span><span class="n">Bold</span><span class="p">)</span>

    <span class="n">keywordPatterns</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;</span><span class="se">\\</span><span class="s">b</span><span class="si">%s</span><span class="se">\\</span><span class="s">b&quot;</span> <span class="o">%</span> <span class="n">k</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">KEYWORDS</span><span class="p">]</span>

    <span class="bp">self</span><span class="o">.</span><span class="n">highlightingRules</span> <span class="o">=</span> <span class="p">[(</span><span class="n">QtCore</span><span class="o">.</span><span class="n">QRegExp</span><span class="p">(</span><span class="n">pattern</span><span class="p">),</span> <span class="n">keywordFormat</span><span class="p">)</span>
      <span class="k">for</span> <span class="n">pattern</span> <span class="ow">in</span> <span class="n">keywordPatterns</span><span class="p">]</span>
</div>
<div class="viewcode-block" id="Highlighter.highlightBlock"><a class="viewcode-back" href="../../ccqw.html#ccqw.fileBrowser.Highlighter.highlightBlock">[docs]</a>  <span class="k">def</span> <span class="nf">highlightBlock</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">text</span><span class="p">):</span>
    <span class="k">for</span> <span class="n">pattern</span><span class="p">,</span> <span class="n">fmt</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">highlightingRules</span><span class="p">:</span>
      <span class="n">expression</span> <span class="o">=</span> <span class="n">QtCore</span><span class="o">.</span><span class="n">QRegExp</span><span class="p">(</span><span class="n">pattern</span><span class="p">)</span>
      <span class="n">index</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">indexIn</span><span class="p">(</span><span class="n">text</span><span class="p">)</span>
      <span class="k">while</span> <span class="n">index</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">:</span>
        <span class="n">length</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">matchedLength</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">setFormat</span><span class="p">(</span><span class="n">index</span><span class="p">,</span> <span class="n">length</span><span class="p">,</span> <span class="n">fmt</span><span class="p">)</span>
        <span class="n">index</span> <span class="o">=</span> <span class="n">expression</span><span class="o">.</span><span class="n">indexIn</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="n">index</span> <span class="o">+</span> <span class="n">length</span><span class="p">)</span>

    <span class="bp">self</span><span class="o">.</span><span class="n">setCurrentBlockState</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>

<span class="c">#===============================================================================</span></div></div>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span>

  <span class="kn">import</span> <span class="nn">sys</span>
  <span class="kn">import</span> <span class="nn">signal</span>
  <span class="n">signal</span><span class="o">.</span><span class="n">signal</span><span class="p">(</span><span class="n">signal</span><span class="o">.</span><span class="n">SIGINT</span><span class="p">,</span> <span class="n">signal</span><span class="o">.</span><span class="n">SIG_DFL</span><span class="p">)</span>

  <span class="n">app</span> <span class="o">=</span> <span class="n">QtGui</span><span class="o">.</span><span class="n">QApplication</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">)</span>
  <span class="n">window</span> <span class="o">=</span> <span class="n">FileBrowser</span><span class="p">(</span><span class="n">path</span><span class="o">=</span><span class="n">__file__</span><span class="p">)</span>
  <span class="n">window</span><span class="o">.</span><span class="n">resize</span><span class="p">(</span><span class="mi">640</span><span class="p">,</span> <span class="mi">480</span><span class="p">)</span>
  <span class="n">window</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
  <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="n">app</span><span class="o">.</span><span class="n">exec_</span><span class="p">())</span>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../index.html">
              <img class="logo" src="../../_static/ogs.png" alt="Logo"/>
            </a></p>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">CCQW 0.1.1 documentation</a> &raquo;</li>
          <li><a href="../index.html" >Module code</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, Roberto Vidmar, Nicola Creati.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
  </body>
</html>